package util;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * 随机生成二维码的工具类
 *
 * maven  添加依赖：
 * <dependency>
 *     <groupId>com.google.zxing</groupId>
 *     <artifactId>core</artifactId>
 *     <version>3.4.1</version>
 * </dependency>
 * <dependency>
 *     <groupId>com.google.zxing</groupId>
 *     <artifactId>javase</artifactId>
 *     <version>3.4.1</version>
 * </dependency>
 *
 */
public class QRCodeGeneratorUtil {
    //用于生成二维码的配置
    //字符集  扫二维码后   显示的内容
    private static final String ALPHA_NUMERIC_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    //二维码宽度
    private static final int QR_CODE_WIDTH = 300;
    //二维码高度
    private static final int QR_CODE_HEIGHT = 300;
    //文件格式
    private static final String FILE_FORMAT = "PNG";

    // 生成随机字符串
    public static String generateRandomString(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(ALPHA_NUMERIC_STRING.length());
            sb.append(ALPHA_NUMERIC_STRING.charAt(index));
        }
        return sb.toString();
    }

    // 生成二维码并保存为文件
    public static void generateQRCodeImage(String text, String filePath) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        // 设置二维码的纠错级别为高
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 生成二维码的位矩阵
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, QR_CODE_WIDTH, QR_CODE_HEIGHT, hints);
        filePath = filePath+"二维码/";
        createDirectoryIfNotExists(filePath);

        // 获取文件路径
        Path path = FileSystems.getDefault().getPath(filePath + UUID.randomUUID()+ ".png");
        // 将位矩阵转换为图像并保存
        MatrixToImageWriter.writeToPath(bitMatrix, FILE_FORMAT, path);
    }

    // 生成随机二维码并保存为文件
    public static void generateRandomQRCodeImage(int randomStringLength, String filePath) throws WriterException, IOException {
        String randomString = generateRandomString(randomStringLength);
        generateQRCodeImage(randomString, filePath);
    }

    /**
     * 创建目录的方法
     * @param directory
     * @throws IOException
     */
    private static void createDirectoryIfNotExists(String directory) throws IOException {
        Path path = Paths.get(directory);
        if (!Files.exists(path)) {
            Files.createDirectories(path);
        }
    }

}
